//
//  TKIMSrvMsgView.m
//  TKIM
//
//  Created by liupm on 15/11/16.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMSrvRichMsgView.h"
#import "TKIMChatView.h"




@interface TKIMSrvRichMsgView()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) TKIMSrvRichMessage *richMsg;

@end



@implementation TKIMSrvRichMsgView

/**
 *  @Author lpm, 15-07-30 11:07:12
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.tableView];
}


-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(TKIMScreenWidth*0.05, 0, TKIMScreenWidth*0.9, 10) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.layer.cornerRadius = 5.0f;
        _tableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _tableView.layer.borderWidth = 0.5f;
        _tableView.bounces = NO;
        _tableView.canCancelContentTouches = NO;
        [_tableView setTableFooterView:[UIView new]];
    }
    return _tableView;
}

/**
 *  @Author lpm, 15-11-17 10:11:25
 *
 *  @brief
 *
 *  @param tableView
 *
 *  @return
 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


/**
 *  @Author lpm, 15-11-17 10:11:31
 *
 *  @brief
 *
 *  @param tableView
 *  @param section
 *
 *  @return
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.richMsg.items.count;
}




/**
 *  @Author lpm, 15-11-17 10:11:38
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 *
 *  @return
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TKIMSrvRichMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKIMSrvRichMsgCell"];
    
    if (!cell)
    {
        cell = [[TKIMSrvRichMsgCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TKIMSrvRichMsgCell" ];

    }
    TKIMSrvRichItem *item = [self.richMsg.items objectAtIndex:indexPath.row];
    
    if (indexPath.row == 0)
    {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell setItem:item type:TKIMSrvRichMsgCellVerticalType];

    }
    else
    {
        [cell setItem:item type:TKIMSrvRichMsgCellHorizontalType];
    }
    return cell;
}


/**
 *  @Author lpm, 15-11-17 10:11:42
 *
 *  @brief
 *
 *  @param tableView
 *  @param indexPath
 *
 *  @return
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 185;
    }
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    TKIMSrvRichItem *item = [self.richMsg.items objectAtIndex:indexPath.row];
    [self routerEventWithName:TKIMSrvTemplOnclick userInfo:item];

}


/**
 *  @Author lpm, 15-07-27 17:07:14
 *
 *  @brief  设置文字
 *
 *  @param text
 */
-(void)setMessage:(TKIMMessage *)message
{
    [super setMessage:message];
    self.richMsg = (TKIMSrvRichMessage *)message;
    self.tableView.frame = CGRectMake(self.tableView.left, 0, self.tableView.width, (self.richMsg.items.count-1)*60+185);
    self.frame =  CGRectMake(self.left, self.top, self.tableView.width, self.tableView.height+TKIMChatMsgPadding);
    self.backgroundImageView.frame = CGRectMake(0, 0, self.width, self.height);
    [self.tableView reloadData];
    message.size = self.frame.size;
    
}


-(TKIMSrvRichMessage *)richMsg
{
    if (!_richMsg)
    {
        _richMsg = [[TKIMSrvRichMessage alloc] init];
    }
    return _richMsg;
}


/**
 *  @Author lpm, 15-11-05 09:11:46
 *
 *  @brief  行高
 *
 *  @return
 */
+(CGFloat)heightForRow:(TKIMMessage *)msg
{
    TKIMSrvRichMessage *rmsg = (TKIMSrvRichMessage *)msg;
    return (rmsg.items.count-1)*60+185+TKIMChatMsgPadding;
}

@end


@interface TKIMSrvRichMsgCell()

@property(nonatomic,strong) UIImageView *picView;
@property(nonatomic,strong) UILabel *titleLabel;
@end

@implementation TKIMSrvRichMsgCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self drawView];
    }
    return self;
}


-(void)drawView
{

    [self.contentView addSubview:self.picView];
    [self.contentView addSubview:self.titleLabel];
}



-(void)setItem:(TKIMSrvRichItem *)item type:(TKIMSrvRichMsgCellType)type
{
    _item = item;
    
    if (type == TKIMSrvRichMsgCellHorizontalType)
    {
        self.titleLabel.frame = CGRectMake(10, 10, TKIMScreenWidth*0.9-70, 40);
        self.picView.frame  = CGRectMake(self.titleLabel.right, 10, 40, 40);
        self.titleLabel.backgroundColor = [UIColor clearColor];
        self.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else
    {
        self.picView.frame  = CGRectMake(10, 10, TKIMScreenWidth*0.9-20, 165);
        self.titleLabel.frame = CGRectMake(self.picView.left,175-60,self.picView.width,60);
        self.titleLabel.backgroundColor = [UIColor blackColor];
        self.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
    self.titleLabel.text = item.title;
    [self.picView sd_setImageWithURL:[NSURL URLWithString:item.picUrl] placeholderImage:TKIMGetImage(@"default_imgbackground")];
}


-(UIImageView *)picView
{
    if (!_picView)
    {
        _picView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, TKIMScreenWidth*0.9-20, 165)];
    }
    return _picView;
}


-(UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.picView.left,185-70,self.picView.width,60)];
        _titleLabel.backgroundColor = [UIColor blackColor];
        _titleLabel.numberOfLines = 2;
        _titleLabel.alpha = 0.8;
        _titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _titleLabel.font = [UIFont systemFontOfSize:16.0f];
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}


@end

